package com.wbcs.system.buttons;

import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.system.ReportRequest;
import com.wbcs.util.Consts_Private;
import com.wbcs.util.Tools;

public class BackButton extends WbcsButton
{
    public BackButton(IComponentConfigBean ccbean)
    {
        super(ccbean);
    }

    @Override
	public String getButtonType()
    {
        return Consts_Private.BACK_BUTTON;
    }

    @Override
	public String showMenu(ReportRequest rrequest,String dynclickevent)
    {
        String clickevent=getClickEvent(rrequest,dynclickevent);
        if(clickevent==null||clickevent.trim().equals(""))
            return "";
        else
            return super.showMenu(rrequest,clickevent);
    }

    @Override
	public String showButton(ReportRequest rrequest,String dynclickevent)
    {
        String clickevent=getClickEvent(rrequest,dynclickevent);
        if(clickevent==null||clickevent.trim().equals(""))
            return "";
        else
            return super.showButton(rrequest,clickevent);
    }

    @Override
	public String showButton(ReportRequest rrequest,String dynclickevent,String button)
    {
        String clickevent=getClickEvent(rrequest,dynclickevent);
        if(clickevent==null||clickevent.trim().equals(""))
            return "";
        else
            return super.showButton(rrequest,clickevent,button);
    }

    private String getClickEvent(ReportRequest rrequest,String dynclickevent)
    {
        if(rrequest.getLstAncestorUrls()==null||rrequest.getLstAncestorUrls().size()==0) return "";
        String clickevent=dynclickevent;
        if(clickevent==null||clickevent.trim().equals(""))
        {
            clickevent=rrequest.getStringAttribute("BACK_ACTION_EVENT","");
            if(clickevent.equals(""))
            {
                String parenturl=rrequest.getLstAncestorUrls().get(0);
                parenturl=Tools.convertBetweenStringAndAscii(parenturl,false);
                if(rrequest.getLstAncestorUrls().size()>1)
                {
                    StringBuffer tempBuf=new StringBuffer();
                    for(int i=1,len=rrequest.getLstAncestorUrls().size();i<len;i++)
                    {
                        tempBuf.append(rrequest.getLstAncestorUrls().get(i)).append("||");
                    }
                    if(tempBuf.toString().endsWith("||"))
                    {
                        tempBuf.delete(tempBuf.length()-2,tempBuf.length());
                    }
                    parenturl=Tools.replaceUrlParamValue(parenturl,"ancestorPageUrls",tempBuf.toString());
                }
                parenturl=Tools.replaceUrlParamValue(parenturl,"refreshComponentGuid","[OUTERPAGE]"+rrequest.getPagebean().getId());
                parenturl=Tools.replaceUrlParamValue(parenturl,"SLAVE_REPORTID",null);
                clickevent="refreshComponent('"+Tools.jsParamEncode(parenturl)+"')";
                rrequest.setAttribute("BACK_ACTION_EVENT",clickevent);
            }
        }
        return clickevent;
    }
}
